package com.slkj.shilixiaoyuanapp.ui.user.activity;

import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.view.CustomStateText;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import butterknife.BindView;
import butterknife.OnClick;

@ActivityInfo(layout = R.layout.xiu_gai_mi_ma, title = "密码更改")
public class ChangePwdActivity extends BaseActivity {
    @BindView(R.id.et_after_pwd)
    EditText etOldPwd;
    @BindView(R.id.et_old_pwd)
    EditText etNewPwd;
    @BindView(R.id.img_old)
    ImageView imgOld;
    @BindView(R.id.et_new_pwd)
    EditText etSureNewPwd;
    @BindView(R.id.img_new)
    ImageView imgNew;
    @BindView(R.id.tv_sure)
    CustomStateText tvSure;
    boolean oldIsPassword, newIsPassword;

    @Override
    protected void init() {
//        etNewPwd.setTransformationMethod(new PasswordTransformationMethod());
//        etSureNewPwd.setTransformationMethod(new PasswordTransformationMethod());
    }


    @OnClick(R.id.img_old)
    public void onImgOldClicked() {
        oldIsPassword = !oldIsPassword;
        etNewPwd.setTransformationMethod(oldIsPassword ? new PasswordTransformationMethod() : null);
        imgOld.setImageResource(oldIsPassword ? R.drawable.ic_eyes_close : R.drawable.ic_eyes_open);
    }

    @OnClick(R.id.img_new)
    public void onImgNewClicked() {
        newIsPassword = !newIsPassword;
        etSureNewPwd.setTransformationMethod(newIsPassword ? new PasswordTransformationMethod() : null);
        imgNew.setImageResource(newIsPassword ? R.drawable.ic_eyes_close : R.drawable.ic_eyes_open);
    }

    @OnClick(R.id.tv_sure)
    public void sure() {
        String old = etOldPwd.getText().toString();
        if (TextUtils.isEmpty(old)){
            showToast("请输入旧密码");
            return;
        }
        String newPz = etNewPwd.getText().toString();
        if (TextUtils.isEmpty(newPz)){
            showToast("请输入新密码");
            return;
        }
        String newPzSure = etSureNewPwd.getText().toString();
        if (TextUtils.isEmpty(newPzSure)){
            showToast("请再次输入新密码");
            return;
        }
        if (!newPz.equals(newPzSure)){
            showToast("两次密码输入不一致");
            return;
        }
        HttpHeper.get().userService()
                .updatePz(old,newPz,newPzSure)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(ChangePwdActivity.this,data);
                        finish();
                    }
                });
    }

    @Override
    protected boolean isShouldHideInput(View v, MotionEvent event) {
        return false;
    }
}
